//index.js
const app = getApp()
const req = require('../../utils/req.js');
const { api } = require('../../utils/api.js');

Page({
  data: {
    hotText: "短衬衫男", // 搜索热词
    currentTab: 0,      // 当前tab
    banner: [],         // 广告位数据
    channel: [],        // 频道数据
    newGoodsList: [],   // 新品列表
    hotGoodsList: [],   // 热卖列表
    brandList: [],      // 品牌列表
    topicList: [],      // 话题列表
    categoryList: [],   // 分类
    newGoodsListTitle: "新品首发",
    hotGoodsListTitle: "热卖专品",
    toView: 't0'
  },
  // 跳转到分类页
	goSortPage() {
    app.jumpToPage('../sort/sort')
    // 其他逻辑
  },
  // 跳转到搜索页面 
  goSearchPage() {
    wx.navigateTo({
      url: './search/search'
    })
  },

  // 切换tab
  switchTab (event) {
    let curTab = event.currentTarget.dataset.currentTab
    this.setData({
      currentTab: curTab,
      toView: 't' + curTab
    })
  },

	// 获取品牌id
	getBrandId(event) {
		let brandId = event.currentTarget.dataset.brandId
		wx.navigateTo({
			url: './brandDetail/brandDetail?id=' + brandId
		})
	},
  
  goGoodsDetail (event) {
    let id = null
    
    if (event.detail.id) {
      id = event.detail.id
    } else {
      id = event.currentTarget.dataset.goodsId
    }

    wx.navigateTo({
      url: './goodsDetail/goodsDetail?id=' + id
    })
	},

  onLoad: function() {

    req.ask(api.index).then(res => {
      let data = res.data.data;
      this.setData({
        banner: data.banner,
        channel: data.channel,
        newGoodsList: data.newGoodsList,
        hotGoodsList: data.hotGoodsList,
        topicList: data.topicList,
        categoryList: data.categoryList
      })
    })

    // 品牌列表
    let brandList = api.brand_list
    brandList.data.page = 1
    brandList.data.size = 15
    
    // 发送请求
    req.ask(brandList).then(res => {
      let data = res.data.data.data
      this.setData({
        brandList: data
      })
    })
  }
  
})